package leetcode.problems;

import java.util.Stack;

/**
 * _0328 Single Number
 * 找出单个数字
 * Created by gmwang on 2018/3/23
 */
public class _0401SingleNumber {
    /**
     * Given an array of integers, every element appears twice except for one. Find that single one.
        Note:
     Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
     */
    /**
     * 思路：
     *
     * @param nums
     * @return
     */
    public static int singleNumber(int[] nums) {
        Stack<Integer> stack = new Stack<>();
        for(int x = 0;x <nums.length;x ++){
            if(stack.contains(nums[x])) {
                stack.remove(stack.indexOf(nums[x]));
            }else{
                stack.push(nums[x]);
            }
        }
        if(stack.size() != 1) return 0;
        return stack.pop();
    }

    public static void main(String[] args) {
        int [] x = {1,2,1,2,3,3,4,5,5,6,6,7,7};
        System.out.println(singleNumber(x));
    }
}
